package com.yuqing.sentimentapp.workspace.festival.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.festival.action.entity.FestivalUnitInfoResult;
import com.yuqing.sentimentapp.workspace.festival.activity.entity.FestivalActivityInfoResult;
import com.yuqing.sentimentapp.workspace.festival.activity.entity.FestivalActivityReportResult;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

/**
 * 疫情防控
 * Created by shenhe on 2020/4/3.
 */
public class FestivalActivityFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.tv_festival)
    TextView mTvFestival;
    @BindView(R.id.ll_festival)
    LinearLayout mLlFestival;
    @BindView(R.id.et_p1)
    EditText mEtP1;
    @BindView(R.id.et_p2)
    EditText mEtP2;
    @BindView(R.id.et_p3)
    EditText mEtP3;
    @BindView(R.id.et_p4)
    EditText mEtP4;
    @BindView(R.id.et_p5)
    EditText mEtP5;
    @BindView(R.id.et_p6)
    EditText mEtP6;

    private boolean editEnable = true;
    private FestivalActivityInfoResult.DataBean actionInfo;
    private ArrayList<FestivalActivityReportResult.DataBean.FestivalsBean> festivals;
    private FestivalActivityReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private FestivalActivityReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, FestivalActivityInfoResult.DataBean actionInfo, ArrayList<FestivalActivityReportResult.DataBean.FestivalsBean> festivals, FestivalActivityReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, FestivalActivityFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("festivals", festivals);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, FestivalActivityReportResult.DataBean.ReportsBean reportsInfo, ArrayList<FestivalActivityReportResult.DataBean.FestivalsBean> festivals) {
        Intent intent = new Intent(activity, FestivalActivityFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        intent.putExtra("festivals", festivals);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_festival_activity_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((FestivalActivityInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        festivals = ((ArrayList<FestivalActivityReportResult.DataBean.FestivalsBean>) intent.getSerializableExtra("festivals"));
        subSum = (FestivalActivityReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum");

        reportsInfo = ((FestivalActivityReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));

    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtP1.setText(actionInfo.getP1());
            mEtP2.setText(actionInfo.getP2());
            mEtP3.setText(actionInfo.getP3());
            mEtP4.setText(actionInfo.getP4());
            mEtP5.setText(actionInfo.getP5());
            mEtP6.setText(actionInfo.getP6());

            if (festivals != null) {
                Observable.fromIterable(festivals).filter(festivalsBean -> TextUtils.equals(actionInfo.getFestival(), festivalsBean.getId())).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(festivalsBean -> {
                            mTvFestival.setText(festivalsBean.getName());
                            mTvFestival.setTag(festivalsBean.getId());
                        });
            }
        } else {
            if (subSum != null) {
                mEtP1.setText(subSum.getP1());
                mEtP2.setText(subSum.getP2());
                mEtP3.setText(subSum.getP3());
                mEtP4.setText(subSum.getP4());
                mEtP5.setText(subSum.getP5());
                mEtP6.setText(subSum.getP6());

                if (festivals != null) {
                    Observable.fromIterable(festivals).filter(festivalsBean -> TextUtils.equals(subSum.getFestival(), festivalsBean.getId())).subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(festivalsBean -> {
                                mTvFestival.setText(festivalsBean.getName());
                                mTvFestival.setTag(festivalsBean.getId());
                            });
                }
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtP1.setText(reportsInfo.getP1());
            mEtP2.setText(reportsInfo.getP2());
            mEtP3.setText(reportsInfo.getP3());
            mEtP4.setText(reportsInfo.getP4());
            mEtP5.setText(reportsInfo.getP5());
            mEtP6.setText(reportsInfo.getP6());

            if (festivals != null) {
                Observable.fromIterable(festivals).filter(festivalsBean -> TextUtils.equals(reportsInfo.getFestival(), festivalsBean.getId())).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(festivalsBean -> {
                            mTvFestival.setText(festivalsBean.getName());
                            mTvFestival.setTag(festivalsBean.getId());
                        });
            }

            mEtP1.setEnabled(false);
            mEtP2.setEnabled(false);
            mEtP3.setEnabled(false);
            mEtP4.setEnabled(false);
            mEtP5.setEnabled(false);
            mEtP6.setEnabled(false);

            mLlFestival.setClickable(false);
            mTvSave.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_save, R.id.ll_festival})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_festival:
                if (festivals == null || festivals.size() == 0) return;
                PopupMenu popupMenu = new PopupMenu(this, mLlFestival);
                Menu menu_more = popupMenu.getMenu();

                for (int i = 0; i < festivals.size(); i++) {
                    menu_more.add(Menu.NONE, i, i, festivals.get(i).getName());
                }
                popupMenu.setOnMenuItemClickListener(menuItem -> {
                    int i = menuItem.getItemId();
                    mTvFestival.setText(festivals.get(i).getName());
                    mTvFestival.setTag(festivals.get(i).getId());
                    return true;
                });
                popupMenu.show();
                break;
            case R.id.tv_save:
                if (TextUtils.isEmpty(mTvFestival.getText().toString())) {
                    showShortToast("请选择节日");
                    return;
                }
                if (actionInfo == null) {
                    actionInfo = new FestivalActivityInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setP1(TextUtils.isEmpty(mEtP1.getText().toString()) ? "0" : mEtP1.getText().toString());
        actionInfo.setP2(TextUtils.isEmpty(mEtP2.getText().toString()) ? "0" : mEtP2.getText().toString());
        actionInfo.setP3(TextUtils.isEmpty(mEtP3.getText().toString()) ? "0" : mEtP3.getText().toString());
        actionInfo.setP4(TextUtils.isEmpty(mEtP4.getText().toString()) ? "0" : mEtP4.getText().toString());
        actionInfo.setP5(TextUtils.isEmpty(mEtP5.getText().toString()) ? "0" : mEtP5.getText().toString());
        actionInfo.setP6(TextUtils.isEmpty(mEtP6.getText().toString()) ? "0" : mEtP6.getText().toString());
    }

    private void updateReportInfo(FestivalActivityInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateFestivalActivityReportInfoResult(actionInfo, mTvFestival.getTag().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(FestivalActivityInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveFestivalActivityReportInfoResult(actionInfo, mTvFestival.getTag().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
